<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Models\BaseModel;
use Illuminate\Support\Facades\Route;

class Goods extends BaseModel
{
    protected $fillable = [
        'name',
        'images_url',
        'price',
        'stock',
        'sell',
        'sort',
        'goods_number',
        'status'
    ];
    // 封面图片获取器
    public function getImagesUrlAttribute($value)
    {
        if(Route::currentRouteName() == 'goods.index'){
            return $value = $value.'?x-oss-process=image/resize,w_40';
        }
        return $value;
    }

    //单价修改器
    public function setPriceAttribute($value)
    {
        $this->attributes['price'] = $value * 100;
    }

    // 单价访问器
    public function getPriceAttribute($value)
    {
        return $value = $value/100;
    }

    // 商品分类关联
    public function classify()
    {
        return $this->belongsToMany('App\Models\GoodsClassify', 'goods_to_classify');
    }
    
//    public function goodsToMaterials()
//    {
//        return $this->hasMany(GoodsToMaterial::class, 'goods_id');
//    }
    
	public function goodsToMaterials()
    {
        return $this->belongsToMany(Material::class, 'goods_to_materials');
    }
}
